public class Test {
    //leetcode 130 被围绕的区域
    private int[] dx = {-1,1,0,0};
    private int[] dy = {0,0,-1,1};
    private boolean[][] vis;
    private int m,n;
    public void solve(char[][] board) {
        m = board.length;
        n = board[0].length;
        vis = new boolean[m][n];
        for (int i = 0; i < n; i++) {
            if (board[0][i] == 'O') dfs(board,0,i);
            if (board[m-1][i] == 'O') dfs(board,m-1,i);
        }
        for (int i = 0; i < m; i++) {
            if (board[i][0] == 'O') dfs(board,i,0);
            if (board[i][n-1] == 'O') dfs(board,i,n-1);
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 'O' && !vis[i][j]) board[i][j] = 'X';
            }
        }
    }


    private void dfs(char[][] board, int i, int j) {
        vis[i][j] = true;
        for (int k = 0; k < 4; k++) {
            int x = i + dx[k];
            int y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O' && !vis[x][y]) {
                dfs(board,x,y);
            }
        }
    }
}
